$(document).ready(function() {
		$("#addissuebtnsave").click(function() {
			
			if( $("#issuetitle").val() == "" ||  $("#bugid").val() == "" || $("#module").chosen().val() == "" ||  $("#issuedetails").val()==""){
				
				alert("Please add Title, Module, Bugid and Details");
				
			}else{
				//var htmltext = CKEDITOR.instances.issuedetails.getData();
				// alert("Content: "+ htmltext);
				var htmltext = $("#issuedetails").val();
				//alert("CAtegpru: "+ $("#category option:selected").text());
				$.ajax({
					type : "post",
					url : "../addissue?action=add",
					datatype : "json",
					data : "issuetitle="+ $("#issuetitle").val()
						+ "&bugid="+ $("#bugid").val()
						+ "&version="+ $("#version").chosen().val()
						+ "&user="+user
						+ "&moduleid="+ $("#module").chosen().val()
						+ "&module="+ $("#module option:selected").text()
						+ "&category="+ $("#category option:selected").text()
						+ "&subcategory="+ $("#subcategory option:selected").text()
						+ "&categoryid="+ $("#category").chosen().val()
						+ "&subcategoryid="+ $("#subcategory").chosen().val()
						+ "&issuedetails="+ escape(htmltext)
						+"&corpuser="+corpusername ,
					success : function(data) {
						if (data.issueinfo.length > 0) {
							var issueid = data.issueinfo[0].issueid
							//alert("Issueid: "+ issueid);
							window.location = "viewissue.jsp?issueid="+ issueid;
						} else {
							Alert("Error saving issue");
						}
				},
				error : function(jqXHR,textStatus) {
					alert("Request failed: "+ textStatus);
				}
			});
		}

			
		
});


		
$.getJSON('../GetCategories?parentid=0', function(data) {
		var i = 0;
		$("#module").html("<option> </option>");
		for (i = 0; i < data.modules.length; i++) {
			$("#module").append('<option value="' + data.modules[i].id+ '">'+ data.modules[i].sectionname + '</option>');
		}
		$("#module").trigger("liszt:updated");
		$("#module").chosen({allow_single_deselect : true});
});
					

$.getJSON('../GetVersions', function(data) {
		var i = 0;
		$("#version").html("<option> </option>");
		for (i = 0; i < data.versions.length; i++) {
			$("#version").append('<option value="' + data.versions[i] + '">'+ data.versions[i] + '</option>');
			// alert("Version: "+data.versions[i] );
		}
		$("#version").trigger("liszt:updated");
});


$("#module").change(function() {
		// alert("You changed value to: " +
		// $("#module option:selected").text() +
		// " moduleid: " + this.value.toString());
		var parentcategory;
		parentcategory = this.value;
		modulename = $("#module option:selected").text();
		moduleid = this.value.toString();
		categoryname = "0";
		categoryid = "0";
		subcategoryid = "0";
		subcategoryid = "0";

		$.getJSON('../GetCategories?parentid='+ parentcategory, function(data) {
			var i = 0;
			$("#category").html("<option> </option>");
			
			for (i = 0; i < data.modules.length; i++) {
				$("#category").append('<option value="'+ data.modules[i].id+ '">'+ data.modules[i].sectionname + '</option>');
			}
			
			$('#category').trigger("liszt:updated");
		});

		$("#subcategory").html("<option> </option>");
		$('#subcategory').trigger("liszt:updated");
});
					

$("#category").change(function() {
	// alert("You changed country to: " + $("#module option:selected").text() + " moduleid: " + this.value.toString());
	var parentcategory;
	
	categoryname = $("#category option:selected").text();
	categoryid = this.value.toString();
	subcategoryid = "0";
	
	parentcategory = this.value;
	
	$.getJSON('../GetCategories?parentid='+ parentcategory, function(data) {
		var i = 0;
		$("#subcategory").html("<option> </option>");

		for (i = 0; i < data.modules.length; i++) {
			$("#subcategory").append('<option value="'+ data.modules[i].id + '">' + data.modules[i].sectionname	+ '</option>');
		}
		$('#subcategory').trigger("liszt:updated");
	});
});


$("#subcategory").change(function() {
		subcategoryname = $("#subcategory option:selected").text();
		subcategoryid = this.value.toString();
		});

});


function getallissues(){
	$("#usertable").jqGrid('setGridParam', {
		url : '../GetIssueList',
		page : 1
	}).trigger("reloadGrid");
}
var selfolderid=0;

$(document).ready(function() {

	$("#jstree").jstree({
		json_data : {
			ajax : {
				url : "../GetTreeData",
				type : "POST",
				dataType : "json",
				contentType : "application/json charset=utf-8",
				success : function(data) {

				}
			}
		},
		"themes" : {
			"theme" : "mytree",
			"dots" : false,
			"icons" : true
		// "url": "/css/jstree/themes/default/style.css"
		},

		"plugins" : [ "themes", "json_data", "ui" ]

	}).bind("select_node.jstree", function(e, data) {
		// alert(data.rslt.obj.data("id"));
		var src = data.rslt.obj.data("id");
		selfolderid = src;
		$("#usertable").jqGrid('setGridParam', {
			url : "../GetIssueList?folderid=" + src,
			page : 1
		}).trigger("reloadGrid");

	});

	$("#search").click(function() {

		var searchfield = jQuery("#corpusername").val();
		$("#usertable").jqGrid('setGridParam', {
			url : "../GetUserRoles?searchfield=" + searchfield,
			page : 1
		}).trigger("reloadGrid");
	});

	
	

	
	
	
	$("#usertable").jqGrid({
		url : '../GetIssueList?',
		datatype : 'json',
		mtype : 'post',
		colNames : [ 'ID', 'Title', 'Version', 'Bugid','CreatedBy' ],
		colModel : [

		{
			name : 'id',
			index : 'id',
			width : 20,
			align : 'center',
			sortable : false,
			resizable : false,
			hidden : true
		}, {
			name : 'title',
			index : 'issuetitle',
			width : 180,
			// hidden : true,
			align : 'left',
			sortable : true,
			resizable : true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}
		}, {

			name : 'version',
			index : 'version',
			width : 80,
			align : 'left',
			sortable : true,
			search: true,
			resizable : true,
			search: true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}

		}, {

			name : 'bugid',
			index : 'bugid',
			width : 80,
			align : 'left',
			sortable : true,
			resizable : true,
			search: true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}

		} , {

			name : 'createdby',
			index : 'createdby',
			width : 80,
			align : 'left',
			sortable : true,
			search: true,
			resizable : true,
			search: true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}

		}],
		pager : $('#pager'),
		altRows : false,
		width : $(window).width() - 300,
		height : $(window).height() - 300,
		rowNum : 10,
		rowList : [ 10, 20, 30 ],
		viewrecords : true,
		caption : "&nbsp;&nbsp;&nbsp;&nbsp;List of Known Issues",
		rownumbers : true,
		multiselect : false,
		multiboxonly : false,
		loadui : "enable",
		loadtext : "Loading issue list ...",
		recordtext : "Viewing {0} - {1} of {2}",
		emptyrecords : "No Records Found!",
		editurl : "../saverole",

		onSelectRow : function(id) {
			// alert("ID: "+ id );
		},
		
		
		ondblClickRow : function(id) {
			window.location = "viewissue.jsp?issueid=" + id;

		}
	});

	$("#usertable").jqGrid('navGrid', "#pager", {
		edit : false,
		add : false,
		del : false,
		search : false
	});
	$("#usertable").jqGrid('inlineNav', "#pager", {
		edit : true,
		add : false,
		del : false,
		search : false
	});
	
	$('#usertable').jqGrid('filterToolbar', { searchOnEnter: true, enableClear: false });
	
	jQuery("#usertable").jqGrid('navGrid','#pager',{del:false,add:false,edit:false,search:false});
	//jQuery("#usertable").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});

	
	
	////  Publish table
	
	$("#publishtable").jqGrid({
		url : '../GetIssueToPublish?',
		datatype : 'json',
		mtype : 'post',
		colNames : [ 'ID', 'Title','IssueDetails','Version', 'Bugid','Added By' ],
		colModel : [

		{
			name : 'id',
			index : 'id',
			width : 20,
			align : 'center',
			sortable : false,
			resizable : true,
			hidden : true
		}, {
			name : 'title',
			index : 'issuetitle',
			width : 80,
			// hidden : true,
			align : 'left',
			sortable : true,
			resizable : true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}
		}
		,{

			name : 'issuedetails',
			index : 'issuedetails',
			width : 100,
			align : 'left',
			sortable : true,
			resizable : true,
			search: true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}
		}
		, {

			name : 'version',
			index : 'version',
			width : 50,
			align : 'left',
			sortable : true,
			search: true,
			resizable : true,
			search: true,
			cellattr : function(rowId, tv, rawObject, cm, rdata) {
				return 'style="white-space: normal;"';
			}

		},{

			name : 'bugid',
			index : 'bugid',
			width : 40,
			align : 'left',
			sortable : true,
			resizable : true,
			search: true,
			

		} 
		,{

			name : 'createdby',
			index : 'createdby',
			width : 40,
			align : 'left',
			sortable : true,
			resizable : true,
			search: true,
			
		}
		
		],
		pager : $('#pager'),
		altRows : false,
		width : $(window).width() - 200,
		height : $(window).height() - 300,
		rowNum : 10,
		rowList : [ 10],
		viewrecords : true,
		caption : "&nbsp;&nbsp;&nbsp;&nbsp;New Issues to Validate",
		rownumbers : true,
		multiselect : true,
		multiboxonly : false,
		loadui : "enable",
		loadtext : "Loading issue list ...",
		recordtext : "Viewing {0} - {1} of {2}",
		emptyrecords : "No Records Found!",
		editurl : "../test",

		onSelectRow : function(id) {
			 //alert("ID: "+ id );
			myid=id;
			//alert("ID: "+ myid );
			 
		},
		
		
		ondblClickRow : function(id) {
			window.location = "viewissue.jsp?issueid=" + id;

		}
	});

	$("#publishtable").jqGrid('navGrid', "#pager", {
		edit : false,
		add : false,
		del : false,
		search : false
	});
	$("#publishtable").jqGrid('inlineNav', "#pager", {
		edit : false,
		add : false,
		del : false,
		search : false,
		save:false,
		cancel:false
		
	});
	
	//$('#publishtable').jqGrid('filterToolbar', { searchOnEnter: true, enableClear: false });
	
	//jQuery("#publishtable").jqGrid('navGrid','#pager',{del:false,add:false,edit:false,search:false});
	//jQuery("#usertable").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
	
	
	


});






